local prefs = {}
local easing = require("easing")

local function MakeFx(t)
    local function fn()
        local inst = CreateEntity()
    
        inst.entity:AddTransform()
        inst.entity:AddAnimState()
        inst.entity:AddNetwork()
        inst.entity:AddSoundEmitter()

        inst:AddTag("FX")
        inst:AddTag("NOCLICK")
        inst:AddTag("NOBLOCK")
        t.fn1(inst)
    
        inst.persists = false
        inst.entity:SetPristine()
        if not TheWorld.ismastersim then
            return inst
        end

        t.fn2(inst)

        return inst
    end

    table.insert(prefs, Prefab(t.name, fn, t.assets))
end

do
    -- 蓝盾
    MakeFx({
        name = "i26ddfx1",
        fn1 = function(inst)
            inst.entity:AddLight()
            inst.Light:SetIntensity(.75)
            inst.Light:SetColour(200 / 255, 150 / 255, 50 / 255)
            inst.Light:SetFalloff(.5)
            inst.Light:SetRadius(6)

            inst.rrr = 3
            inst:DoPeriodicTask(FRAMES, function()
                inst.rrr = inst.rrr - .1
                inst.Light:SetRadius(inst.rrr)
            end,FRAMES)

            inst.AnimState:SetBank("abigail_shield")
            inst.AnimState:SetBuild("abigail_shield")
            inst.AnimState:PlayAnimation("shield")
            --inst.AnimState:SetFinalOffset(1)
            inst.AnimState:SetSortOrder(1)
        end,
        fn2 = function(inst)
            inst.ATC = function(inst, player)
                inst.entity:SetParent(player.entity) 
                inst.Transform:SetPosition(0,-1,0)
                return inst
            end
            inst:ListenForEvent("animover", inst.Remove)
        end,
    })
    -- 红盾
    MakeFx({
        name = "i26ddfx2",
        fn1 = function(inst)
            inst.entity:AddLight()
            inst.Light:SetIntensity(.75)
            inst.Light:SetColour(200 / 255, 150 / 255, 50 / 255)
            inst.Light:SetFalloff(.5)
            inst.Light:SetRadius(6)

            inst.rrr = 3
            inst:DoPeriodicTask(FRAMES, function()
                inst.rrr = inst.rrr - .1
                inst.Light:SetRadius(inst.rrr)
            end,FRAMES)

            inst.AnimState:SetBank("abigail_shield")
            inst.AnimState:SetBuild("abigail_shield")
            inst.AnimState:PlayAnimation("shield_retaliation")
            inst.AnimState:SetSortOrder(1)
        end,
        fn2 = function(inst)
            inst.ATC = function(inst, player)
                inst.entity:SetParent(player.entity) 
                inst.Transform:SetPosition(0,-1,0)
                return inst
            end
            inst:ListenForEvent("animover", inst.Remove)
        end,
    })
    -- 蓝盾3
    MakeFx({
        name = "i26ddfx3",
        fn1 = function(inst)
            inst.entity:AddLight()
            inst.Light:SetIntensity(.75)
            inst.Light:SetColour(200 / 255, 150 / 255, 50 / 255)
            inst.Light:SetFalloff(.5)
            inst.Light:SetRadius(6)

            inst.Light:SetRadius(3)

            inst.AnimState:SetBank("abigail_shield")
            inst.AnimState:SetBuild("abigail_shield")
            inst.AnimState:PlayAnimation("shield")
            --inst.AnimState:SetFinalOffset(1)
            inst.AnimState:SetSortOrder(1)
            inst.AnimState:SetMultColour(1, 1, 1, .6)
        end,
        fn2 = function(inst)
            inst.ATC = function(inst, player)
                inst.entity:SetParent(player.entity) 
                inst.Transform:SetPosition(0,-1,0)
                return inst
            end
        end,
    })
    -- 爆炸 
    MakeFx({
        name = "i26bfx1",
        fn1 = function(inst)
            inst._faderadius = 2
            inst.entity:AddLight()
            inst.Light:SetColour(220/255, 100/255, 0/255)
            inst.Light:SetRadius(inst._faderadius)
            inst.Light:SetFalloff(.9)
            inst.Light:SetIntensity(.8)
            inst.Light:Enable(true)
            inst.Light:EnableClientModulation(true)

            inst.Transform:SetScale(1.5,1.5,1.5)
            inst.AnimState:SetBank("deer_fire_charge")
            inst.AnimState:SetBuild("deer_fire_charge")
            inst.AnimState:PlayAnimation("blast")
            inst.SoundEmitter:PlaySound("dontstarve/common/deathpoof")

            inst._fadeframes = 15
            inst.curf = -15
            local function OnUpdateFade(inst)
                inst.curf = inst.curf + 1
                local k
                if inst._fadeframes <= 0 then
                    k = (inst.curf + 15) / 15
                else
                    k = (15 - inst.curf) / 15
                end
            
                inst.Light:SetRadius(inst._faderadius * k)

            
                if inst.curf >= inst._fadeframes then
                    inst.Light:Enable(false)
                    inst._fadetask:Cancel()
                    inst._fadetask = nil
                end
            end
            inst._fadetask = inst:DoPeriodicTask(FRAMES, OnUpdateFade)
        end,
        fn2 = function(inst)
            inst:ListenForEvent("animover", inst.Remove)
        end,
    })
    -- 手持发光
    MakeFx({
        name = "i26faguangfx",
        fn1 = function(inst)
            inst.AnimState:SetBank("swap_i26laotie")
            inst.AnimState:SetBuild("swap_i26laotie")
            inst.AnimState:PlayAnimation("fx", true)
        end,
        fn2 = function(inst)
            inst.ATC = function(inst, player)
                inst.entity:SetParent(player.entity) 
                inst.Transform:SetPosition(0,-1,0)
                return inst
            end
            --inst:ListenForEvent("animover", inst.Remove)
            inst:DoTaskInTime(5, inst.Remove)
        end,
    })
end


return unpack(prefs)